package org.itboys.mobile.service.mongo.web.product;

import org.apache.ibatis.annotations.Param;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBookProduct;
import org.itboys.mobile.entity.mongo.product.KitchenProductCookBook;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.web.cookbook.KitchenCookBookService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_菜谱_食材_service
 */
/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_商品_关联_菜谱_service。
 *
 */
@Service
public class KitchenProductCookBookService extends BaseMongoService<KitchenProductCookBook> {
    @Autowired
    private KitchenCookBookService cookBookService;



    @Resource(name = "kitchenDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }
    @Override
    protected Class<KitchenProductCookBook> getEntityClass() {
        return KitchenProductCookBook.class;
    }

    /**
     * 分页获取商品关联的菜谱
     * @param request
     * @return
     */
    public PageResult<KitchenCookBook> selectCookBook(HttpServletRequest request, Long productId){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("isDeleted", 0);
        param.put("productId",productId);
        List<Long> cookBookIds = new ArrayList<Long>();
        PageResult<KitchenCookBook> result = new PageResult<KitchenCookBook>();
        List<KitchenProductCookBook> productCookBooks=super.list(param);
        for(KitchenProductCookBook productCookBook:productCookBooks){
            cookBookIds.add(productCookBook.getCookBookId());
        }
        if(ListUtil.isNull(cookBookIds))return result;

        param.clear();
        param.put("id in",cookBookIds);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"name","viceName");
        result = cookBookService.containpageQuery(request,containsparam,param);
        return result;
    }


    /**
     *  新增--商品-关联的食谱
     * @param productId
     * @param cookBookIds
     */
    public void saveProductCookBook(Long productId,List<Long> cookBookIds ){
        List<KitchenProductCookBook> needToAdd=new ArrayList<KitchenProductCookBook>();
        if(ListUtil.isNull(cookBookIds))return;
        for(Long cookBookId:cookBookIds){
            needToAdd.add(new KitchenProductCookBook(productId,cookBookId));
        }
        super.batchSave(needToAdd);
    }

    /**
     * 删除--商品-关联的食谱
     * @param cookBookId
     */
    public void deleteProductCookBook(Long cookBookId ){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("isDeleted", 0);
        param.put("cookBookId",cookBookId);
        super.delete(param);
    }

}
